package com.BXuan.LeetCode.数组.数组_二维数组;

import java.util.ArrayList;
import java.util.List;

public class Solution {

    /**
     * 48.旋转图像
     * 二维数组的特性，现根据对角线进行翻转，然后再转行操作
     */
    public void rotate(int[][] matrix) {
        int m = matrix.length;

        for (int i = 0; i < m; i++){
            for (int j = i; j < m; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        for (int[] arr : matrix){
            reserveRow(arr);
        }
    }
    public void reserveRow(int[] arr){
        int i  = 0;
        int j = arr.length - 1;
        while (i < j){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }

    /**
     * 48.旋转图像
     * 二维数组的特性，现根据对角线进行翻转，然后再转行操作
     */
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;

        int shang = 0;
        int xia = m - 1;
        int zuo = 0;
        int you = n - 1;

        List<Integer> list = new ArrayList<>();

        while (list.size() < m * n){
            if (shang <= xia){
                for (int i = zuo; i <= you; i++){
                    list.add(matrix[shang][i]);
                }
                shang++;
            }
            if (you >= zuo){
                for (int i = shang; i <= xia; i++){
                    list.add(matrix[i][you]);
                }
                you--;
            }
            if (xia >= shang){
                for (int i = you; i >= zuo ; i--){
                    list.add(matrix[xia][i]);
                }
                xia--;
            }
            if (zuo <= you){
                for (int i = xia; i >= shang; i--){
                    list.add(matrix[i][zuo]);
                }
                zuo++;
            }
        }
        return list;
    }

    /**
     * 59. 螺旋矩阵 II
     */
    public int[][] generateMatrix(int n) {
        int shang = 0;
        int xia = n - 1;
        int zuo = 0;
        int you = n - 1;

        int[][] res = new int[n][n];

        int total = 1;
        while (total <= n * n){
            if (shang <= xia){
                for (int i = zuo; i <= you; i++){
                    res[shang][i] = total++;
                }
                shang++;
            }
            if (you >= zuo){
                for (int i = shang; i <= xia; i++){
                    res[i][you] = total++;
                }
                you--;
            }
            if (xia >= shang){
                for (int i = you; i >= zuo; i--){
                    res[xia][i] = total++;
                }
                xia--;
            }
            if (zuo <= you){
                for (int i = xia; i >= shang; i--){
                    res[i][zuo] = total++;
                }
                zuo++;
            }
        }
        return res;
    }

    /**
     * 剑指 Offer 29. 顺时针打印矩阵
     */
    public int[] spiralOrder02(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return new int[]{};
        }

        int m = matrix.length;
        int n = matrix[0].length;

        int shang = 0;
        int xia = m - 1;
        int zuo = 0;
        int you = n - 1;

        int[] res = new int[m * n];

        int total = 0;

        while (total < m * n){
            if (shang <= xia){
                for (int i = zuo; i <= you; i++){
                    res[total++] = matrix[shang][i];
                }
                shang++;
            }
            if (you >= zuo){
                for (int i = shang; i <= xia; i++){
                    res[total++] = matrix[i][you];
                }
                you--;
            }
            if (xia >= shang){
                for (int i = you; i >= zuo ; i--){
                    res[total++] = matrix[xia][i];
                }
                xia--;
            }
            if (zuo <= you){
                for (int i = xia; i >= shang; i--){
                    res[total++] = matrix[i][zuo];
                }
                zuo++;
            }
        }
        return res;
    }

}
